package com.hxl.olympic.fragment;

import android.os.Build;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.google.gson.Gson;
import com.hxl.okhttp.response.JsonArrayResponseHandler;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.hxl.olympic.R;
import com.hxl.olympic.activity.MainActivity;
import com.hxl.olympic.adapter.LvAdapter;
import com.hxl.olympic.adapter.TypeAdapter;
import com.hxl.olympic.adapter.carousel.RecyclerViewBannerAdapter;
import com.hxl.olympic.pojo.Goods;
import com.hxl.olympic.util.Constant;
import com.hxl.olympic.util.DemoDataProvider;
import com.xuexiang.xui.widget.banner.recycler.BannerLayout;
import com.xuexiang.xui.widget.button.RippleView;
import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.xuexiang.xui.widget.textview.marqueen.MarqueeFactory;
import com.xuexiang.xui.widget.textview.marqueen.MarqueeView;
import com.xuexiang.xui.widget.textview.marqueen.SimpleNoticeMF;
import com.xuexiang.xui.widget.toast.XToast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import pl.droidsonroids.gif.GifImageView;

public class HomeFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.img_side)
    public GifImageView imgSide;
    @BindView(R.id.img_scan)
    public ImageView imgScan;
    @BindView(R.id.mv_marquee)
    MarqueeView mvMarquee;
    @BindView(R.id.bl_horizontal)
    BannerLayout blHorizontal;
    @BindView(R.id.rv_search)
    RippleView rvSearch;
    @BindView(R.id.layout_refresh)
    SmartRefreshLayout layoutRefresh;
    @BindView(R.id.gv)
    GridView gv;
    @BindView(R.id.lv)
    ListView lv;
    private RecyclerViewBannerAdapter mAdapterHorizontal;
    private String[] names;
    private int[] icons;
    private TypeAdapter typeAdapter;
    private LvAdapter lvAdapter;
    private List<Goods> goods = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void initialize() {
        //竖向滚动TextView
        final List<String> datas = Arrays.asList("   北京", "   张家口", "   延庆", "   残奥会", "   冬奥会");
        MarqueeFactory<TextView, String> marqueeFactory1 = new SimpleNoticeMF(getContext());
        marqueeFactory1.setOnItemClickListener(new MarqueeFactory.OnItemClickListener<TextView, String>() {
            @Override
            public void onItemClick(View view, MarqueeFactory.ViewHolder<TextView, String> holder) {
            }
        });
        mvMarquee.setMarqueeFactory(marqueeFactory1);
        mvMarquee.startFlipping();
        mvMarquee.setInterval(3800);
        marqueeFactory1.setData(datas);

        //首页轮播图加载图片地址
        blHorizontal.setAdapter(mAdapterHorizontal = new RecyclerViewBannerAdapter(DemoDataProvider.urls));

        names = new String[]{"天气环境", "在线互译", "汇率转换", "周边景点", "赛事详情", "赛区介绍", "实时路况", "全部分类"};
        icons = new int[]{R.mipmap.ic_type_fushi, R.mipmap.ic_type_meizhuang, R.mipmap.ic_type_jiadian, R.mipmap.ic_type_shouji,
                R.mipmap.ic_type_wenti, R.mipmap.ic_type_yundong, R.mipmap.ic_type_shenghuo, R.mipmap.ic_type_qita,
        };

        lvAdapter = new LvAdapter(activity, goods);
        lv.setAdapter(lvAdapter);
//        selectGoods("all");
        setHeight();
    }

    public void setHeight() {
    }

    @Override
    @OnClick({R.id.img_side, R.id.img_scan, R.id.rv_search, R.id.mv_marquee})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_side:
                ((MainActivity) activity).openSide();
                break;
            case R.id.img_scan:
                getRuntimePermission();
                break;
            case R.id.rv_search:
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        SearchFragment searchFragment = new SearchFragment();
                        searchFragment.setType(Constant.GOODS_TYPE);
                        fragmentManager.beginTransaction().replace(R.id.layout_main, searchFragment)
                                .addToBackStack(null)
                                .commit();
                    }
                }, 500);
                break;
        }
    }

    public void selectGoods(String input) {
        dialog.show();
        Map<String, String> params = new HashMap<>();
        params.put("input", input);
        app.getOkHttpUtils().post(Constant.BASE_URL + Constant.queryGoods, params, new JsonArrayResponseHandler() {
            @Override
            public void onSuccess(int statusCode, JSONArray response) {
                try {
                    goods.clear();
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject data = (JSONObject) response.get(i);
                        Goods good = new Gson().fromJson(data.toString(), Goods.class);
                        goods.add(good);
                    }
                    lvAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.cancel();
            }

            @Override
            public void onFailure(int statusCode, String error_msg) {
                if (statusCode == 200) {
                    XToast.error(activity, "查询不到商品数据!", Toast.LENGTH_SHORT).show();
                    dialog.cancel();
                } else {
                    XToast.error(activity, "网络请求失败!", Toast.LENGTH_SHORT).show();
                }
                dialog.cancel();
            }
        });
    }

    @Override
    public void initData() {
        mAdapterHorizontal.setOnBannerItemClickListener(position -> {
            XToast.normal(getContext(), "点击了第" + (position + 1) + "个", Toast.LENGTH_SHORT).show();
        });

        typeAdapter = new TypeAdapter(activity, names, icons);
        gv.setAdapter(typeAdapter);
        //分类点击事件
        gv.setOnItemClickListener((parent, view, position, id) -> {
            switch (position) {
                case 7:
                    fragmentManager.beginTransaction().replace(R.id.layout_main, new TypeFragment())
                            .addToBackStack(null)
                            .commit();
                    break;
                default:
//                    String input = names[position];
//                    SelectGoodsFragment selectGoodsFragment = new SelectGoodsFragment();
//                    selectGoodsFragment.setInput(input);
//                    fragmentManager.popBackStack();
//                    fragmentManager.beginTransaction().replace(R.id.layout_main, selectGoodsFragment)
//                            .addToBackStack(null)
//                            .commit();
                    break;
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initViews() {

    }

}
